﻿using ConfExpert.Utility;
using DevTeam;
using Extensions;
using System;
using System.Data;

namespace ConfOrder.Utility
{
    public partial class Brands : XForm
    {
        public Brands()
        {
            InitializeComponent();
        }

        private void Brands_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;
            btnSave.ItemClick += btnSave_ItemClick;
            gridView1.Init("GetBrands", 75);
            gridView1.SetIdentity("BrandId");
            gridView1.DataTable().AddAutoIncrementalColumn("BrandId", -1, -1);

            gridView1.Columns["BrandId"].ColumnEdit = null;
            gridView1.MakeColumnInvisible("Abbr", "CustomerId", "InsertedOn");
            gridView1.Columns["Deleted"].Visible = true;
            gridView1.RowDeleting += gridView1_RowDeleting;
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView1.DataTable().TableNewRow += Brands_TableNewRow;
            gridView1.MoveLast();
            gridView1.AddSummary();
            gridView1.BestFitColumns();
        }

        void gridView1_RowDeleting(object sender, DevExpress.Data.RowDeletingEventArgs e)
        {
            e.Cancel = true;
        }

        void Brands_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["CustomerId"] = 75;

        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string Brand = string.Empty;
            if (gridView1.GetFocusedDataRow() != null)
                Brand = gridView1.GetFocusedDataRow()["Name"].ToString();

            gridView1.EndEdit();

            foreach (DataRow r in gridView1.DataTable().Rows)
                if (r["Abbr"].ToStringNN() == string.Empty)
                    r["Abbr"] = r["Name"];

            gridView1.Update("Name,Abbr,CustomerId,BrandId", "Brand");

            gridView1.VeriyiGuncelle(true, "GetBrands", 75);

            int rh = gridView1.LocateByValue("Name", Brand);
            gridView1.MakeRowVisible(rh);

            if (rh == 0)
                gridView1.MoveLast();

            AppSession.MainForm.RefreshData();
        }
    }
}
